package com.admission.web;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.admission.model.ArrayInfo;
import com.admission.model.Special;
import com.admission.model.SpecialArticle;
import com.admission.model.SpecialAttachment;
import com.admission.model.SpecialSite;
import com.admission.service.SpecialArticleManager;
import com.admission.service.SpecialAttachmentManager;
import com.admission.service.SpecialManager;
import com.admission.service.SpecialSiteManager;
import com.admission.util.FileUtils;
import com.admission.util.PageUtils;
import com.opensymphony.xwork2.ActionContext;

@Namespace("/base")
@Results({ @Result(name = CrudActionSupport.RELOAD, location = "/base/index.action", type = "redirect") })
public class SpecialArticleAction extends CrudActionSupport<SpecialArticle> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private SpecialArticle entity;
	private SpecialArticleManager specialArticleManager;
	private SpecialAttachmentManager specialAttachmentManager;
	private SpecialSiteManager specialSiteManager;
	private String id;
	private Page<SpecialArticle> page = new Page<SpecialArticle>(
			PageUtils.DEFAULT_PAGE_SIZE);
	private Page<SpecialAttachment> attachmentPage = new Page<SpecialAttachment>(
			PageUtils.DEFAULT_PAGE_SIZE);
	private Page<SpecialSite> sitePage = new Page<SpecialSite>(
			PageUtils.DEFAULT_PAGE_SIZE);
	private String keyword;
	private String spec_id;
	private String specialName;
	private String attachmentName;
	private String siteName;
	private String value = "";
	private Special special;
	private SpecialManager specialManager;
	//private String status;// 判断文章，附件，链接

	public SpecialArticle getModel() {
		return entity;
	}

	// 异步查询访问人数
	public String visitorCount() throws IOException {
		String path = ServletActionContext.getServletContext().getRealPath(
				"/WEB-INF");
		String r = "";
		path += "/other/visitCount.properties";
		File f = new File(path);
		if (f.exists()) {
			f.createNewFile();
		}
		ActionContext actionContext = ActionContext.getContext();
		Map session = actionContext.getSession();
		if (session.get(id) == null) {
			r = FileUtils.getProperties(path, id, "1", true);
			session.put(id, "1");
		} else {
			r = FileUtils.getProperties(path, id, "1", false);
		}

		Struts2Utils.renderText(r);
		return null;
	}

	@Override
	public String list() throws Exception {
		special = specialManager.getSpecialById(spec_id);
		specialName = specialArticleManager.specialName(ArrayInfo.SPECIAL);
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		if (!StringUtils.isEmpty(spec_id)) {
			PropertyFilter p = new PropertyFilter("EQS_special.id", spec_id);
			filters.add(p);
		}
		if (!StringUtils.isEmpty(keyword)) {
			PropertyFilter p1 = new PropertyFilter("LIKES_title", keyword);
			filters.add(p1);
		}
		if (!StringUtils.isEmpty(value)) {
			PropertyFilter p2 = new PropertyFilter("EQS_tag", value);
			filters.add(p2);
		}
		page.setOrder(Page.DESC);
		page.setOrderBy("hightop");
		page.setOrderBy("inputTime");
		
		specialArticleManager.getPageByFilter(page, filters);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		specialName = specialArticleManager.specialName(ArrayInfo.SPECIAL);
		attachmentName = specialArticleManager.specialName(ArrayInfo.SPECIALATTACHMENT);
		siteName = specialArticleManager.specialName(ArrayInfo.SPECIALSITE);
		// 推荐文章
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		PropertyFilter p = new PropertyFilter("EQS_special.id", spec_id);
		PropertyFilter p1 = new PropertyFilter("EQS_tag", SpecialArticle.TAGTRUE);
		filters.add(p);
		filters.add(p1);
		page.setOrder(Page.DESC);
		page.setOrderBy("inputTime");
		specialArticleManager.getPageByFilter(page, filters);
		// 推荐附件
		List<PropertyFilter> filters1 = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		PropertyFilter p2 = new PropertyFilter("EQS_special.id", spec_id);
		filters1.add(p2);
		attachmentPage.setOrder(Page.DESC);
		attachmentPage.setOrderBy("inputTime");
		specialAttachmentManager.getByFilter(attachmentPage, filters1);
		// 推荐链接
		sitePage.setOrder(Page.DESC);
		sitePage.setOrderBy("inputTime");
		specialSiteManager.getByFilter(sitePage, filters1);
		return INPUT;
	}

	public String artShow() throws Exception {
		specialName = specialArticleManager.specialName(ArrayInfo.SPECIAL);
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		if (!StringUtils.isEmpty(keyword)) {
			PropertyFilter p1 = new PropertyFilter("LIKES_title", keyword);
			filters.add(p1);
		}
		// 推荐文章
		PropertyFilter p = new PropertyFilter("EQS_special.id",spec_id);
		filters.add(p);
		page.setOrder(Page.DESC);
		page.setOrderBy("inputTime");
		specialArticleManager.getPageByFilter(page, filters);
		return "artshow";
	}

	public String attShow() throws Exception {
		attachmentName = specialArticleManager.specialName(ArrayInfo.SPECIALATTACHMENT);
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		if (!StringUtils.isEmpty(keyword)) {
			PropertyFilter p1 = new PropertyFilter("LIKES_title", keyword);
			filters.add(p1);
		}
		// 推荐附件
		PropertyFilter p = new PropertyFilter("EQS_special.id",spec_id);
		filters.add(p);
		attachmentPage.setOrder(Page.DESC);
		attachmentPage.setOrderBy("inputTime");
		specialAttachmentManager.getByFilter(attachmentPage, filters);

		return "attshow";
	}

	public String siteShow() throws Exception {
		siteName = specialArticleManager.specialName(ArrayInfo.SPECIALSITE);
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		if (!StringUtils.isEmpty(keyword)) {
			PropertyFilter p1 = new PropertyFilter("LIKES_title", keyword);
			filters.add(p1);
		}
		// 推荐链接
		PropertyFilter p = new PropertyFilter("EQS_special.id",spec_id);
		filters.add(p);
		sitePage.setOrder(Page.DESC);
		sitePage.setOrderBy("inputTime");
		specialSiteManager.getByFilter(sitePage, filters);
		return "siteshow";
	}

	@Override
	public String save() throws Exception {
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		return RELOAD;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isEmpty(id))
			entity = new SpecialArticle();
		else
			entity = specialArticleManager.getById(id);

	}

	public SpecialArticle getEntity() {
		return entity;
	}

	public void setEntity(SpecialArticle entity) {
		this.entity = entity;
	}

	public SpecialArticleManager getSpecialArticleManager() {
		return specialArticleManager;
	}

	@Autowired
	public void setSpecialArticleManager(
			SpecialArticleManager specialArticleManager) {
		this.specialArticleManager = specialArticleManager;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Page<SpecialArticle> getPage() {
		return page;
	}

	public void setPage(Page<SpecialArticle> page) {
		this.page = page;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public String getSpec_id() {
		return spec_id;
	}

	public void setSpec_id(String spec_id) {
		this.spec_id = spec_id;
	}

	public String getSpecialName() {
		return specialName;
	}

	public void setSpecialName(String specialName) {
		this.specialName = specialName;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public Special getSpecial() {
		return special;
	}

	public void setSpecial(Special special) {
		this.special = special;
	}

	public SpecialManager getSpecialManager() {
		return specialManager;
	}

	@Autowired
	public void setSpecialManager(SpecialManager specialManager) {
		this.specialManager = specialManager;
	}

	public Page<SpecialAttachment> getAttachmentPage() {
		return attachmentPage;
	}

	public void setAttachmentPage(Page<SpecialAttachment> attachmentPage) {
		this.attachmentPage = attachmentPage;
	}

	public Page<SpecialSite> getSitePage() {
		return sitePage;
	}

	public void setSitePage(Page<SpecialSite> sitePage) {
		this.sitePage = sitePage;
	}

	public SpecialAttachmentManager getSpecialAttachmentManager() {
		return specialAttachmentManager;
	}

	@Autowired
	public void setSpecialAttachmentManager(
			SpecialAttachmentManager specialAttachmentManager) {
		this.specialAttachmentManager = specialAttachmentManager;
	}

	public SpecialSiteManager getSpecialSiteManager() {
		return specialSiteManager;
	}

	@Autowired
	public void setSpecialSiteManager(SpecialSiteManager specialSiteManager) {
		this.specialSiteManager = specialSiteManager;
	}

	public String getAttachmentName() {
		return attachmentName;
	}

	public void setAttachmentName(String attachmentName) {
		this.attachmentName = attachmentName;
	}

	public String getSiteName() {
		return siteName;
	}

	public void setSiteName(String siteName) {
		this.siteName = siteName;
	}


}
